//
//  GradeView.swift
//  SwiftUIDemo
//
//  Created by zhouzuosong on 2020/9/22.
//  Copyright © 2020 Kael. All rights reserved.
//

import SwiftUI

class GradeViewGroupModel: NSObject {
    var groupID : String? = ""
    var groupName : String? = ""
    var gradeList : [GradeModel]?
    
}

class GradeModel: NSObject {
    var gradeID : String? = ""
    var gradeName : String = ""
    var selected : Bool = false
}

struct GradeView: View {
    var gradeList : [GradeViewGroupModel]?
    
    @State var name = false
//    @Binding var selectedID = "id0"
    
    var body: some View {
        
        ZStack {
            Color.red
            VStack {
                BannerText(pageTitle: "年级组件", text: "根据数据动态显示年级信息", backgroundColor: .accentColor)
                
                Button("年级") {
                    
                }
                .padding()
                
                ScrollView(.vertical, showsIndicators: /*@START_MENU_TOKEN@*/true/*@END_MENU_TOKEN@*/, content: {
                    
                    ForEach (0 ..< 3) { item in
                        let gradeGroup = prepareData()
                        GradeGroupItemView(groupModel: gradeGroup?[item])
                    }
                    
                })
                .frame(maxWidth:.infinity)
                .background(Color.white)
                .cornerRadius(8.0)
                
                Spacer()
            }
        }
        
    }
    
    func prepareData() -> [GradeViewGroupModel]? {
        
        var gradeGroupList = [GradeViewGroupModel]()
        
        for item in 0...2 {
            let groupModel = GradeViewGroupModel()
            groupModel.groupName = "学段\(item+1)"
            groupModel.groupID = "id\(item)"
            
            for grade in 0...5 {
                let gradeModel = GradeModel()
                gradeModel.gradeName = "\(grade+1)年级"
                gradeModel.gradeID = "id\(grade)"
                groupModel.gradeList?.append(gradeModel)
            }
            
            gradeGroupList.append(groupModel)
            
        }
        
        return gradeGroupList
    }
    
    func pppppp() -> Void {
        
    }
}

struct GradeGroupItemView : View {

    @State var groupModel : GradeViewGroupModel?
    @State var gradeModel = GradeModel()
    
    var themeColro = Color(red: 50/256, green: 190/256, blue: 100/256)
    var unSelectedColro = Color(red: 248/256, green: 248/256, blue: 248/256)

    var body: some View {
        VStack{
            
            HStack{
                Circle()
                    .foregroundColor(.green)
                    .frame(width: 8, height: 8, alignment: .center)
                Text("\(groupModel?.groupName ?? "年级名称:")")
                    .foregroundColor(Color(red: 102/256, green: 102/256, blue: 102/256))
                    .font(.system(size: 12))
                Spacer()
            }
            .padding()
            .background(Color.white)
            
            HStack{
                
                Spacer()

                Button(action: {
                    if ((gradeModel.gradeID) != nil) {
                        gradeModel.gradeName = "七年级"
                        gradeModel.gradeID = "id7"
                    }
                    else{
                        gradeModel.gradeName = "八年级"
                        gradeModel.gradeID = "id8"
                    }
                    
                }, label: {
                    Text("改年级")
                        .foregroundColor(.white)
                        .font(.subheadline)
                })
                .frame(minWidth:60,minHeight: 24)
                .background(
                    Color.green
                        .cornerRadius(12)
                )

                Spacer()

                Button(action: {
                    
                }, label: {
                    Text("年级12345667777")
                        .foregroundColor(.white)
                        .font(.subheadline)
                })
                .frame(minWidth:60,minHeight: 24)
                .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))
                .background(
                    Color.green
                        .cornerRadius(12)
                )

                Spacer()
                
//                GradeItemView(gradeModel: $gradeModel)
                ForEach (0 ..< 3) { item in
                    
                }
                GradeItemView(gradeID: "Eight Grade", gradeName: "Eight Grade", gradeSelected: false)
                
                Spacer()
                
            }
            
            Spacer()

        }
    }
    
}

struct GradeItemView:View {
    
    @State var gradeID : String
    @State var gradeName: String
    @State var gradeSelected : Bool
    
    var themeColor = Color(red: 50/256, green: 190/256, blue: 100/256)
    var unSelectedColro = Color(red: 248/256, green: 248/256, blue: 248/256)
    
    var body: some View{
        Button(action: {
            print("kael button selected")
        }, label: {
            Text("\((gradeName.count > 0) ? gradeName : "年级")")
                .foregroundColor(gradeSelected ? .white : .black)
                .font(.system(size: 13))
//                .foregroundColor(.orange)
                


        })
        .frame(minWidth: 75, minHeight: 28)
        .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))
        .background(
            gradeSelected ? themeColor.cornerRadius(14) : unSelectedColro.cornerRadius(14)
        )
        .buttonStyle(GradeButtonStyle())
        

        
    }
}

struct GradeButtonStyle: ButtonStyle {
    var themeColor = Color(red: 50/256, green: 190/256, blue: 100/256)
    var unSelectedColro = Color(red: 80/256, green: 220/256, blue: 140/256)
    
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .background(configuration.isPressed ? themeColor.cornerRadius(14) : unSelectedColro.cornerRadius(14))
    }
}

struct GradeView_Previews: PreviewProvider {
    static var previews: some View {
        GradeView()
    }
}
